package UIWindows;

import Objetos.Objeto;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class jPaneNewObject extends javax.swing.JDialog implements ActionListener{

    static Objeto crearObjeto() {
        jPaneNewObject jdnobj = new jPaneNewObject();
        return jdnobj.getObj();
    }

    
    private final JTextField tNombre;
    private final JTextField tPosX;
    private final JTextField tPosY;
    private final JTextField tPosZ;
    private final ButtonGroup ButGr = new ButtonGroup();
    private final JRadioButton radBFriend = new JRadioButton ("Propio");
    private final JRadioButton radBEnemy = new JRadioButton ("Hostil");
    private final JRadioButton radBNeutral = new JRadioButton ("Neutral");
    private final JRadioButton radBUnkown = new JRadioButton ("Desconocido");
    private final JButton Bok = new JButton("OK");
    private final JButton Bcancelar = new JButton("Cancelar");
    private Objeto obj;
    
    
    private jPaneNewObject(){
        tNombre = new JTextField("");
        tNombre.setColumns(50);
        tPosX=new JTextField("");
        tPosX.setColumns(10);
        tPosY = new JTextField("");
        tPosY.setColumns(10);
        tPosZ = new JTextField("");
        tPosZ.setColumns(10);
        
        //grupo de radiobuttons
        ButGr.add(radBFriend);
        ButGr.add(radBEnemy);
        ButGr.add(radBNeutral);
        ButGr.add(radBUnkown);
        radBFriend.setSelected(true);
        
        //crear etiquetas
        JLabel lblDescripcion= new JLabel("Descripción del objeto");
        JLabel lblPosX= new JLabel("X");
        JLabel lblPosY= new JLabel("Y");
        JLabel lblPosZ= new JLabel("Z");
        
        //escuchadores
        Bok.addActionListener(this);
        Bcancelar.addActionListener(this);
        
        //crear paneles
        JPanel panelDescripcion = new JPanel();
        JPanel panelPosicion = new JPanel();
        JPanel panelRadioButtons = new JPanel();
        JPanel panelButtonsButtons = new JPanel();
        
        //organizar controles en paneles
        this.setLayout(new FlowLayout());
        this.add(panelDescripcion);
        panelDescripcion.add(lblDescripcion);
        panelDescripcion.add(tNombre);
        
        this.add(panelPosicion);
        panelPosicion.add(lblPosX);
        panelPosicion.add(tPosX);
        panelPosicion.add(lblPosY);        
        panelPosicion.add(tPosY);
        panelPosicion.add(lblPosZ);        
        panelPosicion.add(tPosZ);
        
        this.add(panelRadioButtons);
        panelRadioButtons.add(radBFriend);
        panelRadioButtons.add(radBEnemy);
        panelRadioButtons.add(radBNeutral);
        panelRadioButtons.add(radBUnkown);
        
        this.add(panelButtonsButtons);
        panelButtonsButtons.add(Bok);
        panelButtonsButtons.add(Bcancelar);
        
        //Colocacion
        this.setModal(true);
        this.setTitle("ENTRADA DE OBJETO");
        this.setSize(800, 200);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(screenSize.width/2 - (this.getWidth()/2), screenSize.height/2 - (this.getHeight()/2)); 
        this.setResizable(false);
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        
        if (ae.getSource()== Bok) { 
                try {
                int tipoObj=0;
                if (this.radBFriend.isSelected())tipoObj = 1;
                if (this.radBEnemy.isSelected())tipoObj = 2;
                if (this.radBNeutral.isSelected())tipoObj = 3;
                if (this.radBUnkown.isSelected())tipoObj = 0;
                this.obj = new Objeto(this.tNombre.getText(),
                        Long.valueOf(this.tPosX.getText()),
                        Long.valueOf(this.tPosY.getText()),
                        Long.valueOf(this.tPosZ.getText()),
                        tipoObj);
                this.dispose();
                }catch (NumberFormatException e){
                    javax.swing.JOptionPane.showMessageDialog(null,"Las coordenadas no son válidas","Datos del punto inválidos",JOptionPane.INFORMATION_MESSAGE);
                }
        }    
        if (ae.getSource()== Bcancelar) { 
            //javax.swing.JOptionPane.showMessageDialog(null,"Click Cancelar","Datos de entrada",JOptionPane.INFORMATION_MESSAGE);
            this.dispose();        
        }
    }

    Objeto getObj() {
        return this.obj;
        
    }
}

   
